<?php
/*
 * IWA assignment 2 - service.php 
 * Maximilian Einoeder
 * Mark Mensing
 */

//Get the input text
$query = $_POST['textQuery'];
$keywords = array();

/*
 * Search for literals in the query using regex
 * The result will be a 2d array. $keywords[0] will contain
 * all words how they were found in the string, so with qoutes. The subarray
 * from $keywords[1] is the one you want, which contains only keywords without 
 * qoutes
 */
preg_match_all('/"([^"]+)"/', $query, $keywords);

//Construct and make a request to the eventful API
$domain = "http://api.eventful.com";
$service = "/rest/events/search?app_key=DnxJRzxJWJShchnt";

$url = $domain . $service . "&keywords=" . $keywords[1][0];

$count = count($keywords[1]);

for($i = 1; $i < $count; $i++) {
    $url = $url . "+" . $keywords[1][$i];
}

//Retrieve the response xml
$xmlEventful = simplexml_load_file($url);


//For now output the description of the first event to show that it works
$description = $xmlEventful->events->event[0]->description;

echo $description;
?>
